<?php
namespace application\datatypes;
use application\datatypes\DataType;

class Password extends DataType {
	public static $name = 'Password';
	public static $fields = array(array('columnType'=>'VARCHAR(200)'));
	
	public static function validate($field,$data, $results) {
		/** This is for signing up or changing password */
		$password1 = $data['user_password_password1'];
		$password2 = $data['user_password_password2'];

		if($password1 != $password2) $results['errors'][] = 'Your new passwords must match.';
		elseif($password1 && strlen($password1) < 6) $results['errors'][] = 'For your security, passwords must be more than 5 characters.';
		elseif(!$password1) $password1 = $data['user_password_current'];
		
		if($password1) $results['db_fields']['user_password'] = md5($password1);
		return $results;	
	}
	
	public static function display($formElements) {
		global $concerto;
		$form = $concerto->queryVars['form'];
		switch($form) {
			case 'login':?>
				
				<fieldset class="password-field">
				<header>Password</header>
				<section class="inner">
				
				<?php echo $formElements['elements']['password1'];?>
				</section>
				</fieldset>
				<?php
				break;
			case 'edit_profile':?>
				<?php if((getQueryVar('id') == $concerto->User->getData('user_id')) || !getQueryVar('id') || getQueryVar('id') && $concerto->User->isAdmin()): ?>
				
				
				<fieldset class="password-field">
				<header>Password</header>
				<section class="inner">
				<?php if($formElements['showCurrentPassword']) : ?>
				<label>Current Password</label>
				<?=$formElements['elements']['current_password']?>
				<?php endif;?>
				<label>New Password</label>
				<?=$formElements['elements']['password1']?>
				<label>Re-Enter New Password</label>
				<?php echo $formElements['elements']['password2'];?>
				</section>
				</fieldset>
				
				<?php endif;
				break;
			case 'add_user':
			case 'signup':?>
				
				<fieldset class="password-field">
				<header>Password</header>
				<section class="inner">
				<label>Password</label>
				<?=$formElements['elements']['password1']?>
				<label>Re-Enter Password</label>
				<?php echo $formElements['elements']['password2'];?>
				</section>
				</fieldset>
				<?php
				break;		
		}

		
	}
	
	public static function build($field, $data) {
		global $concerto;
		$formElements = array('description'=>$field->cf_description, 'column'=>$column, 'display'=>$field->cf_display_name, 'data_type'=>'Password', 'elements'=>array(
			'current_password'=>
				sprintf('<input type="password" autocomplete="off" class="long-text password-text" name="%s" id="%s"/>', $field->cf_column.'_current', $field->cf_column.'_current'),
			'password1'=>
				sprintf('<input type="password" autocomplete="off" class="long-text password-text" name="%s" id="%s"/>', $field->cf_column.'_password1', $field->cf_column.'_password1'),
			'password2'=>
				sprintf('<input type="password" autocomplete="off" class="long-text password-text" name="%s" id="%s"/>', $field->cf_column.'_password2', $field->cf_column.'_password2'),
				
			));
		if($data['user_password']) {
			$formElements['showCurrentPassword'] = TRUE;
		}
		
		if($concerto->User->isAdmin() && (getQueryVar('id') && getQueryVar('id') != $concerto->User->getData('user_id'))) {
			$formElements['showCurrentPassword'] = FALSE;
		}
		return $formElements;
	
	}
	
	public static function prepareData($field, $data, $params) {
		return FALSE;
	}
	
	public static function fromDB($fields, $field, $data) {
		$column = $field->cf_column;
		return array($field->cf_column.'_password1' => $fields[$column], $field->cf_column.'_password2'=>$fields[$column], $field->cf_column => $fields[$column]);
	}
	
}
?>